package com.hentie.gamelib;

import java.io.IOException;
import java.util.Random;

import org.w3c.dom.DOMException;

import android.content.Context;
import android.util.Log;

import com.hentie.drawlib.Camera;
import com.hentie.drawlib.OpenGLRenderer;
import com.hentie.inputlib.Input;
import com.hentie.mathlib.Vector2;
import com.hentie.mathlib.Vector3;
import com.hentie.spritedrawlib.Sprite;
import com.hentie.spritedrawlib.SpriteFactory;
import com.hentie.spritelib.SpriteData;
import com.hentie.spritelib.SpriteXMLParser;
import com.hentie.tilelib.Level;

public class PlatformGame extends Game {

	public PlatformGame(Context context, OpenGLRenderer renderer) {
		super(context, renderer);
		// TODO Auto-generated constructor stub
	}

	float rot = 0;
	int spriteAmount = 10;
	Sprite[] testSprite = new Sprite[spriteAmount];
	Vector3[] pos = new Vector3[spriteAmount];
	Vector3 scale = new Vector3(0.2f,0.2f,0.2f);
	Vector2 currentInput = Vector2.Zero();
	SpriteData spriteDat;
	Level testLevel;
	float x = 0;
	
	
	@Override
	public void Initialize()
	{
		spriteDat = SpriteXMLParser.ParseXML(ctx,"Sprites/Goku/Goku.xml");
		Random rand = new Random();
		for(int i = 0; i < spriteAmount; i++)
		{
			testSprite[i] = new Sprite(ctx,spriteDat);
			pos[i] = new Vector3(-rand.nextFloat()*100f +50,-rand.nextFloat()*100f + 50f,-80);
			testSprite[i].play("IDLE", -1);
		}
		try {
			testLevel = Level.ParseLevelFromXML(ctx, "Levels/TestLevel/herp2.tmx");
		} catch (DOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	}
	
	@Override
	public void Update(float deltaTime)
	{
		rot+=199.1f*deltaTime;
		for(int i = 0; i < spriteAmount; i++)
		{
		  testSprite[i].Update(deltaTime, pos[i], rot, scale);
		}

		
		
		
		if(Input.touchState.GetFirstFinger().IsTouching())
		{
			currentInput = Input.touchState.GetFirstFinger().GetTouchVector();
			//Log.v("Input Test", currentInput.toString());
		}
		
		
	}
	
	@Override
	public void Draw(OpenGLRenderer renderer, float deltaTime)
	{
		for(int i = 0; i < spriteAmount; i++)
		{
		testSprite[i].Draw(this, deltaTime);
		
		}
		testLevel.Draw(this, deltaTime);
		//Must be inserted at the end
		super.Draw(renderer, deltaTime);
		
	}
	
	
	
}
